export default{
  data(){
    return {
      showPickup:false
    }
  },
  methods:{
    openPickup(){
      console.log(this.viewer);
      // 创建一个ScreenSpaceEventHandler实例来处理鼠标事件
      this.handler = new Cesium.ScreenSpaceEventHandler(this.viewer.canvas);
      // 注册左键单击事件
      this.handler.setInputAction(this.pickupHandler, Cesium.ScreenSpaceEventType.LEFT_CLICK);
    },
    pickupHandler(click){
      setTimeout(()=>{
        let cartesian = null;
        // 获取鼠标点击的屏幕坐标
        const pickedObject = this.viewer.scene.pick(click.position);
        if (pickedObject && pickedObject.primitive && pickedObject.primitive.attribute && pickedObject.primitive.attribute.resourceId) {
          cartesian = this.viewer.scene.pickPosition(click.position);
          if (Cesium.defined(cartesian)){
            console.log(cartesian,'pickup');
            const cartographic = Cesium.Cartographic.fromCartesian(cartesian);
            const lon = Cesium.Math.toDegrees(cartographic.longitude);
            const lat = Cesium.Math.toDegrees(cartographic.latitude);
            const height = cartographic.height;
            console.log({lon,lat,height},'pickup经纬度');
            this.addPickupPopup(cartesian)
          } else {
            console.log('Cartesian position is undefined.');
          }
        }
      },100)
    },
    addPickupPopup(cartesian){
      let element = this.$refs.pickupPopup.$el;
      let option = {
        element: element,
        position: cartesian,
        offset: {
          x: 0, // 向屏幕左边平移的像素值
          y: 0 // 向屏幕下方平移的像素值
        }
      };
      try {
        // 尝试创建并添加Overlay，如果出现异常则捕获并处理
        this.currOverlay = new Cesium.Overlay(option);
        this.viewer.addOverlay(this.currOverlay);
        this.currOverlay.show();
      } catch (error) {
        // 统一处理Overlay创建或显示时可能出现的异常
        this.$message.error('添加建筑详情弹窗时发生错误: ' + error.message);
      }
    },
    closePickup(){
      this.handler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK)
      this.currOverlay && this.currOverlay.destroy();
      this.currOverlay = null;
    }
  },
  created(){
    this.handler = null
  }
}